package cn.mw.cmdb.entity;

import com.alibaba.nacos.common.utils.CollectionUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.*;

import static cn.mw.cmdb.processor.zabbix.ZabbixUtils.substringItemName;

/**
 * 单个组件布局信息
 */
@Data
public class MwLayoutDetailInfo {
    @Schema(description = "id")
    protected String id;

    @Schema(description = "前端布局信息")
    protected LayoutGrid layoutGrid;

    @Schema(description = "布局名称")
    private String componentName;

    @Schema(description = "布局类型")
    private String componentType;

    @Schema(description = "监测数据")
    private MwLayoutItemDropInfo displayData;


    public Set<String> getItemNames() {
        Set<String> wholeItemNameSet = new HashSet<>();
        if (displayData != null) {
            //获取所有的监控项名称
            //需要区分高级表格和普通组件，高级表格需要interface+itemName
            if (CollectionUtils.isNotEmpty(displayData.getInterfaceDrops())) {
                for (String interfaceName : displayData.getInterfaceDrops()) {
                    for (String itemName : displayData.getItemDrop()) {
                        String wholeItemName = interfaceName + itemName;
                        wholeItemNameSet.add(wholeItemName);
                    }
                }
            } else {
                if (CollectionUtils.isNotEmpty(displayData.getItemDrop())) {
                    for (String itemName : displayData.getItemDrop()) {
                        String wholeItemName = itemName;
                        wholeItemNameSet.add(wholeItemName);
                    }
                }
            }
        }
        return wholeItemNameSet;
    }

    /**
     * 根据itemName设置itemInfo数据
     *
     * @param itemInfoMap
     */
    public void setItemIdByName(Map<String, Object> itemInfoMap) {
        if (displayData != null) {
            Set<String> wholeItemNameSet = new HashSet<>();
            List<ItemDropInfo> itemInfoList = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(displayData.getInterfaceDrops())) {
                for (String interfaceName : displayData.getInterfaceDrops()) {
                    for (String itemName : displayData.getItemDrop()) {
                        String wholeItemName = interfaceName + itemName;
                        wholeItemNameSet.add(wholeItemName);
                    }
                }
            } else {
                for (String itemName : displayData.getItemDrop()) {
                    String wholeItemName = itemName;
                    wholeItemNameSet.add(wholeItemName);
                }
            }
            itemInfoMap.forEach((k, v) -> {
                String itemName = k;
                List<String> itemIds = new ArrayList<>();
                if (v != null && v instanceof List) {
                    itemIds = (List) v;
                }
                String name = substringItemName(itemName);
                if (CollectionUtils.isNotEmpty(displayData.getItemInfos())) {
                    for (ItemDropInfo info : displayData.getItemInfos()) {
                        if (name.equals(info.getName())) {
                            info.setItemids(itemIds);
                        }
                    }
                }
            });
        }
    }

}
